package com.metadata_system.system.service;


import com.alibaba.fastjson.JSON;
import com.metadata_system.common.db.entity.Queue;
import com.metadata_system.common.db.mapper.QueueMapper;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.dto.QueueDataItem;
import com.metadata_system.common.utils.DateTimeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class QueueService {
    @Autowired(required = false)
    QueueMapper queueMapper;


    public static String addData(String data, QueueDataItem queueDataItem){
        List<QueueDataItem> dataList=new ArrayList<QueueDataItem>(){};
        if (!StringUtils.isBlank(data)){
            dataList= JSON.parseArray(data, QueueDataItem.class);
        }
        queueDataItem.setCreatedTime(new Date());
        dataList.add(queueDataItem);
        return JSON.toJSONString(dataList);
    }

    /**
     * 创建执行队列
     * @param queue
     * @return
     */
    public ApiResult createQueue(Queue queue) {
        Example example = new Example(Queue.class);
        example.createCriteria()
                .andEqualTo("userId", queue.getUserId())
                .andEqualTo("type",queue.getType())
                .andEqualTo("execParams",queue.getExecParams())
                .andEqualTo("status",0);
        if (queueMapper.selectCountByExample(example)>0) {
            return ApiResult.error("当前有相同类型的队列 已经存在");
        }

        if(queue.getType().equals("ImportProjectNodeData")){
            queue.setExecData(addData("",QueueDataItem.builder()
                    .content("文件上传成功,数据总条数: 9523条,sheet总页数: 3页,文件大小: 115.38M")
                    .offset(0)
                    .build()));
        }


        Queue insertQueue = new Queue();
        insertQueue.setStatus(0);
        insertQueue.setUserId(queue.getUserId());
        insertQueue.setType(queue.getType());
        insertQueue.setInfo(queue.getInfo());
        insertQueue.setExecParams(queue.getExecParams());
        insertQueue.setExecData(queue.getExecData());
        insertQueue.setResultParams("{}");
        queue.setCreatedTime(new Date());
        queue.setUpdatedTime(new Date());
        queueMapper.insert(insertQueue);
        return ApiResult.success(queue.getId());
    }


    /**
     * 更新执行队列
     * @param id
     * @param queueDataItem
     * @return
     */
    public ApiResult updateQueue(Integer id,QueueDataItem queueDataItem) {
        Queue queue = queueMapper.selectByPrimaryKey(id);
        if (queue==null){
            return ApiResult.error("队列不存在");
        }
        String execData = queue.getExecData();
        execData=addData(execData,queueDataItem);
        queue.setExecData(execData);
        queue.setUpdatedTime(new Date());
        queueMapper.updateByPrimaryKeySelective(queue);
        return ApiResult.success();
    }
}
